package com.rd.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rd.pojo.Dept;
import com.rd.pojo.Job;
import com.rd.service.IDeptService;
import com.rd.service.IJobService;
import com.rd.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jianan
 * @since 2024-06-06
 */
@RestController
@RequestMapping("/job")
public class JobController {
    @Autowired
    private IJobService jobService;
    @Autowired
    private IDeptService deptService;

    @GetMapping("/list")
    public R list(@RequestParam("page") int page,
                  @RequestParam("limit") int limit) {
        //1.创建分页插件工具
        Page<Job> pageUtil = new Page<>(page, limit);
        //2.调用IService接口提供page方法进行分页查询
        Page<Job> jobPage = jobService.page(pageUtil);
        //3.获取总记录个数
        long total = jobPage.getTotal();
        //4.获取List集合(存储页面展示的数据)
        List<Job> list=jobPage.getRecords();
        //5.处理list集合
        if(list!=null&&list.size()>0){
            //遍历list集合
            for (Job job : list) {
                //给Job类中刚刚设置的 Dept dept;属性赋值
                Dept dept = deptService.getById(job.getDeptid());
                job.setDept(dept);
            }
        }
        //6.封装R对象返回给前端页面
        return  R.ok().setData(list).setCount(total);

    }
    @GetMapping("/listAll")
    public R listAll(){
        //数据
        List<Job> list = jobService.list();
        return  R.ok().setData(list);
    }
}
